﻿using System;
using Lypshare.Net.Client;
using Lypshare.Flow.Packets;

namespace Lypshare.Flow
{
	/// <summary>
	/// Flow listener.
	/// </summary>
	/// <exception cref='ArgumentNullException'>
	/// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
	/// </exception>
    public class FlowListener : IDisposable
    {
        // --------------------------------------------------------
        // Class Variables
        // --------------------------------------------------------


        /// <summary>
        /// The _session.
        /// </summary>
        private INetworkSession _session = null;


        /// <summary>
        /// The _flow key.
        /// </summary>
        private String _flowKey = null;


        /// <summary>
        /// The _active.
        /// </summary>
        private bool _active = true;


        // --------------------------------------------------------
        // Class Properties
        // --------------------------------------------------------


        /// <summary>
        /// Gets the session.
        /// </summary>
        /// <value>
        /// The session.
        /// </value>
        public INetworkSession Session
        {
            get { return _session; }

        } // Property: Session


        /// <summary>
        /// Gets the flow key.
        /// </summary>
        /// <value>
        /// The flow key.
        /// </value>
        public String FlowKey
        {
            get { return _flowKey; }

        } // Property: FlowKey


        /// <summary>
        /// Gets a value indicating whether this <see cref="Lypshare.Flow.FlowListener"/> is active.
        /// </summary>
        /// <value>
        /// <c>true</c> if active; otherwise, <c>false</c>.
        /// </value>
        public bool Active
        {
            get { return _active; }

        } // Property: Active


        // --------------------------------------------------------
        // Class Functions
        // --------------------------------------------------------


        /// <summary>
        /// Initializes a new instance of the <see cref="Lypshare.Flow.FlowListener"/> class.
        /// </summary>
        /// <param name='session'>
        /// Session.
        /// </param>
        /// <param name='flowKey'>
        /// Flow key.
        /// </param>
        /// <exception cref='ArgumentNullException'>
        /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> .
        /// </exception>
        public FlowListener(INetworkSession session, String flowKey)
        {
            if (session == null) throw new ArgumentNullException("session");
            if (flowKey == null) throw new ArgumentNullException("flowKey");

            _session = session;
            _flowKey = flowKey;

            _session.DataReceived += OnSessionDataReceived;

        } // Constructor


        /// <summary>
        /// Releases all resource used by the <see cref="Lypshare.Flow.FlowListener"/> object.
        /// </summary>
        /// <remarks>
        /// Call <see cref="Dispose"/> when you are finished using the <see cref="Lypshare.Flow.FlowListener"/>.
        /// The <see cref="Dispose"/> method leaves the <see cref="Lypshare.Flow.FlowListener"/> in an unusable
        /// state. After calling <see cref="Dispose"/>, you must release all references to the
        /// <see cref="Lypshare.Flow.FlowListener"/> so the garbage collector can reclaim the memory that the
        /// <see cref="Lypshare.Flow.FlowListener"/> was occupying.
        /// </remarks>
        public void Dispose()
        {
            _active = false;

            _session = null;
            _flowKey = null;

            _session.DataReceived -= OnSessionDataReceived;

        } // Dispose


        // --------------------------------------------------------
        // Class Handles
        // --------------------------------------------------------


        /// <summary>
        /// Raises the session data received event.
        /// </summary>
        /// <param name='s'>
        /// S.
        /// </param>
        /// <param name='e'>
        /// E.
        /// </param>
        protected virtual void OnSessionDataReceived(INetworkSession s, ConnectionDataEventArgs e)
        {
            if (e.Packet is FlowPacket)
            {
            } // if (e.Packet is FlowPacket)
        } // Constructor

    } // class FlowListener
} // namespace Lypshare.Flow